package com.hengtuo.fragment;


import android.os.Handler;
import android.os.Message;
import android.support.v4.widget.SwipeRefreshLayout;
import android.util.Log;
import android.view.View;


import android.widget.ListView;
import android.widget.ProgressBar;
import android.widget.Toast;

import com.hengtuo.adapter.MoveballAdapter;
import com.hengtuo.base.BaseFragment;
import com.hengtuo.kaiyuanzhongguo.R;
import com.hengtuo.util.ObtainNetwork;
import com.hengtuo.util.Url;
import com.hengtuo.view.RefreshLayout;
import com.hengtuo.vo.Moveball;
import com.hengtuo.vo.Tweet;
import com.hengtuo.vo.User;
import com.thoughtworks.xstream.XStream;

import java.util.List;

/**
 * Created by hebingkang on 2016/4/12.
 */
public class Moveball_show_Fragment extends BaseFragment {
    int pageIndex = 0;
    private ListView lv;
    private RefreshLayout myRefreshListView;
    private ProgressBar progressbar = null;
    private List<Tweet> tween;

    private String id;
    Handler mHandler = new Handler() {

        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);

            tween = (List<Tweet>) msg.obj;
            MoveballAdapter adapter = new MoveballAdapter(getActivity(), tween);
            lv.setAdapter(adapter);

        }
    };

    @Override
    protected int getLoadViewId() {

        return R.layout.fragment_moveball_show;
    }

    @Override
    protected void initView(View view) {

        lv = (ListView) view.findViewById(R.id.lv_moveball);

        myRefreshListView = (RefreshLayout) view.findViewById(R.id.swipe_layout);

    }

    @Override
    protected void initData() {
        id = getArguments().getString("MOVEBALLUID");

        getData();

        refreshLord();
    }

    private void refreshLord() {

        myRefreshListView
                .setColorSchemeResources(android.R.color.holo_blue_bright, android.R.color.holo_green_light,
                        android.R.color.holo_orange_light, android.R.color.holo_red_light);


        // 设置下拉刷新监听器
        myRefreshListView.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {

                Toast.makeText(getActivity(), "refresh", Toast.LENGTH_SHORT).show();

                myRefreshListView.postDelayed(new Runnable() {

                    @Override
                    public void run() {

                        getData();
                        // 更新完后调用该方法结束刷新
                        myRefreshListView.setRefreshing(false);
                    }
                }, 1000);
            }
        });

        myRefreshListView.setOnLoadListener(new RefreshLayout.OnLoadListener() {
            @Override
            public void onLoad() {




            }
        });

    }

    private void getData() {
        new Thread() {
            @Override
            public void run() {

                String str = ObtainNetwork.getJsonFromWeb(Url.HOST + "?uid=" + id + "&pageIndex=" + pageIndex + "&pageSize=20", getActivity());
                XStream stream = new XStream();
                stream.processAnnotations(Moveball.class);

                Moveball moveball = (Moveball) stream.fromXML(str);

                tween = moveball.getTweets().getTweet();

                Message msg = Message.obtain();
                msg.obj = tween;
                mHandler.sendMessage(msg);


            }
        }.start();


    }


}
